package com.CST.config;

import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
 * <p></p>
 *
 * @author: xnd
 * @create: 2021-10-26 10:36
 **/
@Configuration
@MapperScan(value = "com.CST.mapper.salary", sqlSessionFactoryRef = "salarySqlSessionFactory")
public class DefaultDataSourceConfig4 {

    @Bean(name = "salaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.hcbhs")
    public DataSource getDateSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "salarySqlSessionFactory")
    public SqlSessionFactory salarySqlSessionFactory(@Qualifier("salaryDataSource") DataSource dataSource)  throws Exception{

        //SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/salary/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "salarySqlSessionTemplate")
    public SqlSessionTemplate salarySqlSessionTemplate(@Qualifier("salarySqlSessionFactory") SqlSessionFactory sessionFactory) {
        return new SqlSessionTemplate(sessionFactory);
    }

}
